package com.xxx.video.network.requests;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import com.xxx.video.network.listeners.CommonResponseListener;
import com.xxx.video.network.listeners.UploadFileListener;

import java.io.IOException;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okio.Buffer;
import okio.BufferedSink;
import okio.ForwardingSink;
import okio.Okio;

public class MultipartRequestBody extends RequestBody {
    /**
     * request body
     */
    private MultipartBody requestBody;
    /**
     * check length
     */
    private long currentLength;
    /**
     * total length
     */
    private long totalLength;
    /**
     * send message tag
     */
    private static final int PROGRESS_MESSAGE = 0x01;
    /**
     * call back
     */
    private UploadFileListener mUploadFileListener;
    /**
     * handler progress call back
     */
    private final Handler mHandler;

    /**
     * method
     *
     * @param requestBody            request body
     * @param commonResponseListener call back
     */
    public MultipartRequestBody(MultipartBody requestBody, CommonResponseListener commonResponseListener) {
        this.requestBody = requestBody;
        this.mUploadFileListener = (UploadFileListener) commonResponseListener;
        mHandler = new Handler(Looper.getMainLooper()) {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case PROGRESS_MESSAGE:
                        /*
                         * handler progress
                         */
                        if (mUploadFileListener != null) {
                            mUploadFileListener.onProgress((Integer) msg.obj);
                        }
                        break;
                    default:
                        break;
                }
            }
        };
    }

    @Override
    public MediaType contentType() {
        return requestBody.contentType();
    }

    @Override
    public long contentLength() throws IOException {
        totalLength = requestBody.contentLength();
        return totalLength;
    }

    @Override
    public void writeTo(BufferedSink sink) throws IOException {
        ForwardingSink forwardingSink = new ForwardingSink(sink) {
            @Override
            public void write(Buffer source, long byteCount) throws IOException {
                super.write(source, byteCount);
                currentLength += byteCount;
                /*
                 * upload progress
                 */
                int progress = (int) (currentLength * 1F / totalLength * 1F * 100F);
                /*
                 * call back progress
                 */
                mHandler.obtainMessage(PROGRESS_MESSAGE, progress).sendToTarget();
            }
        };
        //parsing
        BufferedSink bufferedSink = Okio.buffer(forwardingSink);
        //write data
        requestBody.writeTo(bufferedSink);
        //flush data
        bufferedSink.flush();
    }
}
